// 字节国际化电商

const res = {
  message: 'success',
  code: 0,
  data: [{ title: 'hello' }],
  name: {
    name1: 'wzy',
  },
  age: [[1, 2], 2],
};

// get(res,'message','data[0].title') // 返回['success','hello']

// 编写get函数实现
function get(obj, ...props) {
  const res = [];
  for (let prop of props) {
    // let keyList = prop.replace(/\[(\d+)\]/g, '.$1').split('.');
    let keyList = prop.split('[').join('.').split(']').join('').split('.');
    console.log(keyList);
    const result = keyList.reduce((obj, key) => {
      return obj[key];
    }, obj);
    res.push(result);
  }
  console.log(res);
}

get(res, 'message', 'data[0].title', 'name.name1', 'age[0][1]');
